package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.BankCardAdapter;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.bean.BankCardBean;
import com.jucai.pppeiyouclient.comm.CommonMethod;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.LoginUtils;
import com.jucai.pppeiyouclient.utils.StringUtil;
import com.jucai.pppeiyouclient.utils.ToastUtils;
import com.jucai.pppeiyouclient.view.dialog.BindSuccessDialog;
import com.jucai.pppeiyouclient.view.popup.CardTypePopup;
import com.jucai.pppeiyouclient.weidget.GetVerTextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BindBankCardActivity extends BaseActivity {
    private static final String TAG = "BindBankCardActivity : ";
    private RecyclerView bankCardRv;
    private View bottomView;
    private LinearLayout bindAdd, bindContent;
    private EditText bindCardholder, bindCardNo, bindPhone, bindVer;
    private TextView bindCardType, bindNext;
    private GetVerTextView bindGetVer;
    private List<BankCardBean> cardList = new ArrayList<>();
    private BankCardAdapter cardAdapter;
    private CardTypePopup cardTypePopup;
    private BindSuccessDialog bindSuccessDialog;
    private String isEmpty;
    private boolean isShow = false;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.common_activity_with_rv);

        cardAdapter = new BankCardAdapter(BindBankCardActivity.this, cardList);
        ((TextView) findViewById(R.id.common_title_text)).setText("银行卡");
        bankCardRv = (RecyclerView) findViewById(R.id.common_view);
        bankCardRv.setLayoutManager(new LinearLayoutManager(BindBankCardActivity.this, LinearLayoutManager.VERTICAL, false));
        bankCardRv.setItemAnimator(new DefaultItemAnimator());
        bankCardRv.setAdapter(cardAdapter);
        bottomView = LayoutInflater.from(BindBankCardActivity.this).inflate(R.layout.footer_bind_card, bankCardRv, false);
        cardAdapter.setBottom(bottomView);
        initBottom();

        cardTypePopup = new CardTypePopup(BindBankCardActivity.this, getWindow(), R.style.animBottomInBottomOut, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cardTypePopup.dismiss();
                switch (view.getId()) {
                    case R.id.popup_credit_card:
                        bindCardType.setText("信用卡");
                        break;

                    case R.id.popup_debit_card:
                        bindCardType.setText("借记卡");
                        break;
                }
            }
        });
        bindSuccessDialog = new BindSuccessDialog(BindBankCardActivity.this);
    }

    private void initBottom() {
        bindAdd = (LinearLayout) bottomView.findViewById(R.id.bind_add);
        bindContent = (LinearLayout) bottomView.findViewById(R.id.bind_content);
        bindContent.setVisibility(View.GONE);
        bindCardholder = (EditText) bottomView.findViewById(R.id.bind_cardholder);
        bindCardNo = (EditText) bottomView.findViewById(R.id.bind_card_no);
        bindPhone = (EditText) bottomView.findViewById(R.id.bind_phone);
        bindVer = (EditText) bottomView.findViewById(R.id.bind_ver);
        bindGetVer = (GetVerTextView) bottomView.findViewById(R.id.bind_get_ver);
        bindCardType = (TextView) bottomView.findViewById(R.id.bind_card_type);
        bindNext = (TextView) bottomView.findViewById(R.id.bind_next);
    }

    @Override
    protected void initData() {
        getUserState();
    }

    private void getUserState() {
        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Accompany_Apply.getLastInfo");
        param.put("member_id", LoginUtils.getMember_id(this));
        param.put("token", LoginUtils.getToken(this));
        CommonRequestNetData.getJsonObjectData(BindBankCardActivity.this, param, true, new CommonRequestNetData.JsonObjectCallBack() {
            @Override
            public void parseJsonObject(JSONObject jsonObject) throws Exception {
                LogUtils.e(TAG + "jsObj = " + jsonObject.toString());
                String realName = jsonObject.has("member_realname") ? jsonObject.getString("member_realname") : "";
                bindCardholder.setText(realName);
            }
        });
    }

    @Override
    protected void loadNetData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Client_CreditCard.getAccount");
        LogUtils.e(TAG + "param = " + param.toString());
        CommonRequestNetData.getJsonObjectData(BindBankCardActivity.this, param,true, new CommonRequestNetData.JsonObjectCallBack() {
            @Override
            public void parseJsonObject(JSONObject jsonObject) {
                LogUtils.e(TAG + "jsObj = " + jsonObject.toString());
                try {
                    cardList = new Gson().fromJson(jsonObject.getJSONArray("credit_info").toString(), new TypeToken<List<BankCardBean>>() {
                    }.getType());
                    cardAdapter.setData(cardList);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private String judgeEmpty() {
        if (TextUtils.isEmpty(bindCardholder.getText().toString())) {
            getUserState();
            return "持卡人不能为空";
        } else if (TextUtils.isEmpty(bindCardNo.getText().toString())) {
            return "卡号不能为空";
        } else if (TextUtils.isEmpty(bindCardType.getText().toString())) {
            return "请选择卡类型";
        } else if (TextUtils.isEmpty(bindPhone.getText().toString())) {
            return "手机号不能为空";
        }else if (!StringUtil.matchTel(bindPhone.getText().toString())) {
            return "请输入正确的手机号";
        } else if (TextUtils.isEmpty(bindVer.getText().toString())) {
            return "验证码不能为空";
        } else {
            return "";
        }
    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        bindAdd.setOnClickListener(this);
        bindCardType.setOnClickListener(this);
        bindGetVer.setOnClickListener(this);
        bindNext.setOnClickListener(this);
        bindSuccessDialog.setOnclickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(BindBankCardActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;

            case R.id.bind_add:
                isShow = !isShow;
                if (isShow) {
                    bindContent.setVisibility(View.VISIBLE);
                } else {
                    bindContent.setVisibility(View.GONE);
                }
                break;

            case R.id.bind_card_type:
                cardTypePopup.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                CommonMethod.setWindowAlpha((float) 0.5, getWindow());
                break;

            case R.id.bind_get_ver:
                if (TextUtils.isEmpty(bindPhone.getText().toString())) {
                    ToastUtils.showToast("手机号不能为空");
                } else if (!StringUtil.matchTel(bindPhone.getText().toString())){
                    ToastUtils.showToast("请输入正确的手机号");
                }else {
                    HashMap<String, String> param = new HashMap<>();
                    param.put("member_phone", bindPhone.getText().toString());
                    param.put("service", "Public_VerifyCode.SendCreditCode");

                    CommonRequestNetData.getJsonStringData(BindBankCardActivity.this, param,true, new CommonRequestNetData.JsonStringCallBack() {
                        @Override
                        public void parseJsonString(String str) {
                            LogUtils.e("get ver ---> str = " + str);
                            ToastUtils.showToast(str);
                        }
                    });
                }
                break;

            case R.id.bind_next:
                isEmpty = judgeEmpty();
                if (TextUtils.equals(isEmpty, "")) {
                    HashMap<String, String> param = new HashMap<>();
                    param.put("member_id", LoginUtils.getMember_id(BindBankCardActivity.this));
                    param.put("token", LoginUtils.getToken(BindBankCardActivity.this));
                    param.put("credit_card", bindCardNo.getText().toString());
                    param.put("credit_bankinfo", bindCardType.getText().toString());
                    param.put("credit_personinfo", bindCardholder.getText().toString());
                    param.put("member_phone", bindPhone.getText().toString());
                    param.put("credit_verifycode", bindVer.getText().toString());
                    param.put("service", "Client_CreditCard.addAccount");

                    CommonRequestNetData.getJsonStringData(BindBankCardActivity.this, param,true, new CommonRequestNetData.JsonStringCallBack() {
                        @Override
                        public void parseJsonString(String str) {
                            LogUtils.e("str = " + str);
                            bindSuccessDialog.show();
                        }
                    });
                } else {
                    ToastUtils.showToast(isEmpty);
                }
                break;

            case R.id.bind_success_sure:
                LogUtils.e("bind refresh");
                loadNetData();
                bindContent.setVisibility(View.GONE);
                isShow = false;
                bindSuccessDialog.dismiss();
                break;

            default:
                break;
        }
    }
}
